package com.dao;

import com.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisDemo {
    public static void main(String[] args) throws IOException{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.获取SqlSession对象
        SqlSession sqlSession=sqlSessionFactory.openSession(true);
        //3.获取Mapper接口的代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //4.执行方法
        String id=userMapper.test();

        System.out.println(id);
        //提交事务
        sqlSession.commit();
        //5.释放资源
        sqlSession.close();
    }
}
